package me.eachann.widget.dashline;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public enum DashLineStyle implements DashLineOp {
    LINE_HORIZONTAL(0) {
        @Override
        public void op(Canvas canvas, int width, int height, int dashWidth, int dashGap, Paint paintLine, Paint mCirclePaint) {
            drawHorizontalDashLine(canvas,0,width,height/2,dashWidth,dashGap,paintLine);
        }
    },
    LINE_VERTICAL(1) {
        @Override
        public void op(Canvas canvas, int width, int height, int dashWidth, int dashGap, Paint paintLine, Paint mCirclePaint) {
            drawVerticalDashLine(canvas,0,height,width/2,dashWidth,dashGap,paintLine);
        }
    },
    LINE_WITH_CIRCLE_HORIZONTAL(2) {
        @Override
        public void op(Canvas canvas, int width, int height, int dashWidth, int dashGap, Paint paintLine, Paint mCirclePaint) {
            int start = 0;
            while (start < width) {
                canvas.drawLine(start, height / 2f, start + dashWidth, height / 2f, paintLine);
                start += (dashGap + dashWidth);
            }
            drawHorizontalDashLine(canvas,0,width,height/2,dashWidth,dashGap,paintLine);
            drawCircle(canvas, width, height, mCirclePaint);
        }


    },
    LINE_WITH_CIRCLE_VERTICAL(3) {
        @Override
        public void op(Canvas canvas, int width, int height, int dashWidth, int dashGap, Paint paintLine, Paint mCirclePaint) {
            drawVerticalDashLine(canvas,0,height,width/2,dashWidth,dashGap,paintLine);
            drawCircle(canvas, width, height, mCirclePaint);
        }


    },
    LINE_WITHOUT_START_WITH_CIRCLE_HORIZONTAL(4) {
        @Override
        public void op(Canvas canvas, int width, int height, int dashWidth, int dashGap, Paint paintLine, Paint mCirclePaint) {
            drawHorizontalDashLine(canvas,width / 2,width,height/2,dashWidth,dashGap,paintLine);
            drawCircle(canvas, width, height, mCirclePaint);
        }
    },
    LINE_WITHOUT_START_WITH_CIRCLE_VERTICAL(5) {
        @Override
        public void op(Canvas canvas, int width, int height, int dashWidth, int dashGap, Paint paintLine, Paint mCirclePaint) {
            drawVerticalDashLine(canvas,height / 2,height,width/2,dashWidth,dashGap,paintLine);
            drawCircle(canvas, width, height, mCirclePaint);
        }
    },
    LINE_WITHOUT_END_WITH_CIRCLE_HORIZONTAL(6) {
        @Override
        public void op(Canvas canvas, int width, int height, int dashWidth, int dashGap, Paint paintLine, Paint mCirclePaint) {
            drawHorizontalDashLine(canvas,0,width/2,height/2,dashWidth,dashGap,paintLine);
            drawCircle(canvas, width, height, mCirclePaint);
        }
    },
    LINE_WITHOUT_END_WITH_CIRCLE_VERTICAL(7) {
        @Override
        public void op(Canvas canvas, int width, int height, int dashWidth, int dashGap, Paint paintLine, Paint mCirclePaint) {
            drawVerticalDashLine(canvas,0,height/2,width/2,dashWidth,dashGap,paintLine);
            drawCircle(canvas, width, height, mCirclePaint);
        }
    };

    private static void drawVerticalDashLine(Canvas canvas, int start, int height, int centerX, int dashWidth, int dashGap, Paint paintLine) {
        while (start < height) {
            canvas.drawLine(centerX, start, centerX, start + dashWidth, paintLine);
            start += (dashGap + dashWidth);
        }
    }

    private static void drawHorizontalDashLine(Canvas canvas, int start, int height, int centerX, int dashWidth, int dashGap, Paint paintLine) {
        while (start < height) {
            canvas.drawLine(start, centerX, start + dashWidth, centerX, paintLine);
            start += (dashGap + dashWidth);
        }
    }

    private static float getRadius(int width, int height, Paint paint) {
        return (Math.min(width, height) - paint.getStrokeWidth() * 2) / 2;
    }

    private static void drawCircle(Canvas canvas, int width, int height, Paint mCirclePaint) {
        float strokeWidth = mCirclePaint.getStrokeWidth();
        int color = mCirclePaint.getColor();
        canvas.drawCircle(width / 2f, height / 2f, getRadius(width, height, mCirclePaint), mCirclePaint);
        mCirclePaint.setStrokeWidth(1);
        mCirclePaint.setStyle(Paint.Style.FILL);
        mCirclePaint.setColor(Color.WHITE);
        canvas.drawCircle(width / 2f, height / 2f, getRadius(width, height, mCirclePaint) - strokeWidth, mCirclePaint);
        mCirclePaint.setStrokeWidth(strokeWidth);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setColor(color);
    }

    private int mStyle;

    DashLineStyle(int style) {
        this.mStyle = style;
    }

    public static DashLineStyle fromStyle(int style) {
        for (DashLineStyle s : DashLineStyle.values()) {
            if (s.getStyle() == style) {
                return s;
            }
        }
        return DashLineStyle.LINE_HORIZONTAL;
    }

    public int getStyle() {
        return mStyle;
    }

}
